package com.xinsoft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.common.exception.BDException;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.CusUpkeepParam;
import com.xinsoft.entity.po.*;
import com.xinsoft.entity.vo.CusUpkeepVo;
import com.xinsoft.mapper.CusUpkeepMapper;
import com.xinsoft.service.*;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author lijie
* @description 针对表【cus_upkeep(保养工单)】的数据库操作Service实现
* @createDate 2023-06-19 11:14:54
*/
@Service
public class CusUpkeepServiceImpl extends ServiceImpl<CusUpkeepMapper, CusUpkeep>
    implements CusUpkeepService{

    @Resource
    CusUpkeepMapper cusUpkeepMapper;

    @Resource
    CusUpkeepOrderEntryService orderEntryService;

    @Resource
    CusUpkeepOrderPartService orderPartService;

    @Resource
    CusDeviceService deviceService;

    @Resource
    SysDocumentNumService sysDocumentNumService;

    @Resource
    CusUpkeepAuditService upkeepAuditService;

    @Override
    public void tempSave(CusUpkeep cusUpkeep) {
        // 暂存有新增和编辑场景
        if(cusUpkeep.getId() == null){
            cusUpkeep.setUpkeepStatus(Const.UPKEEP_STATUS_TEMPSAVE);
            // 更新计划单号
            sysDocumentNumService.incrementLastSerialByDocumentId(Const.DOCUMENT_ID.UPKEEP_ORDER_DOCUMENT);
            cusUpkeepMapper.insert(cusUpkeep);
            // 解析执行设备项目关系表
            if(cusUpkeep.getOrderEntryList() != null){
                this.dealOrderList(cusUpkeep);
            }else{
                throw new BDException("操作失败，不包含设备项目集合！");
            }
            // 解析执行备件表
            if(cusUpkeep.getPartList() != null){
                this.dealPartList(cusUpkeep);
            }
        }else{
            if(cusUpkeep.getOrderEntryList() != null){
                orderEntryService.remove(new QueryWrapper<CusUpkeepOrderEntry>().eq("upkeep_id",cusUpkeep.getId()));
                this.dealOrderList(cusUpkeep);
            }else{
                throw new BDException("操作失败，不包含设备项目集合！");
            }
            if(cusUpkeep.getPartList() != null){
                orderPartService.remove(new QueryWrapper<CusUpkeepOrderPart>().eq("upkeep_id",cusUpkeep.getId()));
                this.dealPartList(cusUpkeep);
            }
            cusUpkeepMapper.updateById(cusUpkeep);
        }
        this.changeDeviceStatus(cusUpkeep);
    }

    @Override
    public void submit(CusUpkeep cusUpkeep) {
        // 提交场景
        cusUpkeep.setUpkeepStatus(Const.UPKEEP_STATUS_WAIT);
        if(cusUpkeep.getId() == null){
            // 更新计划单号
            sysDocumentNumService.incrementLastSerialByDocumentId(Const.DOCUMENT_ID.UPKEEP_ORDER_DOCUMENT);
            cusUpkeepMapper.insert(cusUpkeep);
            // 解析执行设备项目关系表
            if(cusUpkeep.getOrderEntryList().size() > 0){
                this.dealOrderList(cusUpkeep);
            }else{
                throw new BDException("操作失败，不包含设备项目集合！");
            }
            // 解析执行备件表
            if(cusUpkeep.getPartList() != null){
                this.dealPartList(cusUpkeep);
            }
        }else{
            if(cusUpkeep.getOrderEntryList().size() > 0){
                orderEntryService.remove(new QueryWrapper<CusUpkeepOrderEntry>().eq("upkeep_id",cusUpkeep.getId()));
                this.dealOrderList(cusUpkeep);
            }else{
                throw new BDException("操作失败，不包含设备项目集合！");
            }
            if(cusUpkeep.getPartList().size() > 0){
                orderPartService.remove(new QueryWrapper<CusUpkeepOrderPart>().eq("upkeep_id",cusUpkeep.getId()));
                this.dealPartList(cusUpkeep);
            }
            cusUpkeepMapper.updateById(cusUpkeep);
        }
        this.changeDeviceStatus(cusUpkeep);
    }

    @Override
    public CusUpkeepVo getByInfoById(Integer id) {
        CusUpkeepVo cusUpkeepVo = cusUpkeepMapper.getInfoById(id);
        // 插入保养情况明细
        List<CusUpkeepOrderEntry> orderEntryList = cusUpkeepMapper.getOrderList(id);
        if(orderEntryList.size() > 0){
            cusUpkeepVo.setOrderEntryList(orderEntryList);
        }
        // 插入使用备件明细
        List<CusUpkeepOrderPart> partList = cusUpkeepMapper.getPartList(id);
        if(partList.size() > 0){
            cusUpkeepVo.setPartList(partList);
        }
        // 添加验收结果对象
        CusUpkeepAudit cusUpkeepAudit = upkeepAuditService.getOne(new QueryWrapper<CusUpkeepAudit>().eq("upkeep_id",id));
        if(cusUpkeepAudit != null){
            cusUpkeepVo.setCusUpkeepAudit(cusUpkeepAudit);
        }
        return cusUpkeepVo;
    }

    @Override
    public Object findPageListByParam(CusUpkeepParam param) {
        //分页
        Page<CusUpkeep> page = new Page<CusUpkeep>(param.getCurrentPage(), param.getPageSize());
        IPage<CusUpkeepVo> resultPage = cusUpkeepMapper.findListByParam(page, param);
        //返回值
        return resultPage;
    }

    @Override
    public void finish(CusUpkeep cusUpkeep) {
        // 提交场景
        cusUpkeep.setUpkeepStatus(Const.UPKEEP_STATUS_OVER);
        if(cusUpkeep.getOrderEntryList().size() > 0){
            orderEntryService.remove(new QueryWrapper<CusUpkeepOrderEntry>().eq("upkeep_id",cusUpkeep.getId()));
            this.dealOrderList(cusUpkeep);
        }else{
            throw new BDException("操作失败，不包含设备项目集合！");
        }
        if(cusUpkeep.getPartList().size() >0){
            orderPartService.remove(new QueryWrapper<CusUpkeepOrderPart>().eq("upkeep_id",cusUpkeep.getId()));
            orderPartService.saveBatch(cusUpkeep.getPartList());
        }
        cusUpkeepMapper.updateById(cusUpkeep);
    }

    @Override
    public CusUpkeepVo getTaskByDeviceId(Integer deviceId) {
        CusUpkeepVo cusUpkeepVo = new CusUpkeepVo();
        List<CusUpkeep> cusUpkeepList = cusUpkeepMapper.selectList(new QueryWrapper<CusUpkeep>()
        .eq("device_id",deviceId)
        .eq("upkeep_status",Const.UPKEEP_STATUS_WAIT)
        .lt("upkeep_start_time", DateUtil.formatDateTime(new Date()))
        .gt("upkeep_end_time",DateUtil.formatDateTime(new Date()))
        .eq("do_delete",Const.DEL_FLAG_NORMAL));
        if(cusUpkeepList.size() > 1){
            throw new BDException("存在同期两条保养执行任务,请前往PC端删除一条");
        }else{
            if(cusUpkeepList.size() > 0){
                BeanUtil.copyProperties(cusUpkeepList.get(0),cusUpkeepVo);
            }else{
                return null;
            }
        }
        return cusUpkeepVo;
    }

    public void dealPartList(CusUpkeep cusUpkeep){
        for (int i = 0; i < cusUpkeep.getPartList().size(); i++) {
            cusUpkeep.getPartList().get(i).setUpkeepId(cusUpkeep.getId());
        }
        orderPartService.saveBatch(cusUpkeep.getPartList());
    }

    public void dealOrderList(CusUpkeep cusUpkeep){
        for (int i = 0; i < cusUpkeep.getOrderEntryList().size(); i++) {
            cusUpkeep.getOrderEntryList().get(i).setUpkeepId(cusUpkeep.getId());
        }
        orderEntryService.saveBatch(cusUpkeep.getOrderEntryList());
    }

    public void changeDeviceStatus(CusUpkeep cusUpkeep){
        // 同步变更设备使用状况
        CusDevice cusDevice = deviceService.getById(cusUpkeep.getDeviceId());
        cusDevice.setUseStatus(cusUpkeep.getDeviceUseStatus());
        deviceService.updateById(cusDevice);
    }


}




